'use strict';

/** signin controller */
app.controller('SigninFormController', function ($http,$rootScope, $scope, $state, $location, ajax, $window) {
    $scope.user = {};
    $scope.authError = null;

    var from = $state.previous;
    $scope.login = function () {
        $scope.authError = null;
        
        var req = new Req.POST({
            resource: 'public/staff/login',
            fields: {
                username: $scope.user.username,
                password: $scope.user.password,
                browser: $window.navigator.userAgent
            }
        });
        ajax.call(req).then(function (result) {
            if (result.code != 200) {
                $scope.authError = result.result;
                return;
            }

            $rootScope.loginUser = result.result;
        /*var data = {
        			params:{
        				fields:{
        						username: $scope.user.username,
        						password: $scope.user.password,
        						browser: $window.navigator.userAgent
        						}
        				}
        			};
        	//post请求的地址
        	var url = "http://127.0.0.1:8080/ecar/r/public/staff/login";
        	//将参数传递的方式改成form
        	
        $http.post(
        	url,data
        ).then(function (result) {
            if (result.data.code != 200) {
                $scope.authError = result.result;
                return;
            }

            $rootScope.loginUser = result.data.result;*/
            $rootScope.$state.isLogin = true;
            $scope.$emit('user.login', {
                uid: $rootScope.loginUser.id,
                uname: $scope.user.username,
            });
            if (from && from.name && from.name.indexOf("signin") < 0) {
                $state.go(from.name);
            } else {
                $location.path('/app/dashboard-v1').replace();
            }
        }, function () {
            $scope.authError = "无法连接到服务器。";
        });
    };

});